package com.escape.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.escape.auth.common.enums.AuthPermissionShowEnum;
import com.escape.auth.common.enums.AuthPermissionStatusEnum;
import com.escape.auth.common.enums.AuthUserStatusEnum;
import com.escape.auth.common.enums.IsDeletedEnum;
import com.escape.auth.domain.convert.AuthPermissionConverter;
import com.escape.auth.domain.entity.AuthPermissionBO;
import com.escape.auth.domain.service.AuthPermissionDomainService;
import com.escape.auth.infra.basic.entity.AuthPermission;
import com.escape.auth.infra.basic.service.AuthPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-10  15:59
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;

    /**
     * 新增权限
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainService.add.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToEntity(authPermissionBO);

        authPermission.setStatus(AuthPermissionStatusEnum.OPEN.getCode());
        authPermission.setShow(AuthPermissionShowEnum.SHOW.getCode());
        authPermission.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());

        Integer count = this.authPermissionService.insert(authPermission);

        return count > 0;
    }

    /**
     * 更新权限
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainService.update.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToEntity(authPermissionBO);
        Integer count = this.authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 删除权限
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainService.delete.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToEntity(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedEnum.DELETED.getCode());

        Integer count = this.authPermissionService.update(authPermission);
        return count > 0;
    }
}
